package com.sandking.db.jedis;

import com.sandking.db.dao.TjzxbbDao;
import com.sandking.db.bean.Tjzxbb;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class TjzxbbJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Tjzxbb getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Tjzxbb tjzxbb = null;
		try{
			String key = "Tjzxbb_Object";
			String field = SK_Plus.b("id:",id).e();
			tjzxbb = Tjzxbb.createForJson(jedis.hget(key,field));
			if(tjzxbb == null){
				tjzxbb = TjzxbbDao.getById(id);
				if(tjzxbb!=null){
					loadCache(tjzxbb);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return tjzxbb;
	}
	

	public static void loadCache(Tjzxbb tjzxbb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(tjzxbb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Tjzxbb tjzxbb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Tjzxbb_Object";
		String field = SK_Plus.b("id:",tjzxbb.getId()).e();
		String data = tjzxbb.toJson();
		p.hset(key,field,data);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Tjzxbb> tjzxbbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Tjzxbb tjzxbb : tjzxbbs){
				loadCache(tjzxbb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Tjzxbb tjzxbb){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(tjzxbb,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Tjzxbb tjzxbb,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Tjzxbb_Object";
		String field = SK_Plus.b("id:",tjzxbb.getId()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Tjzxbb> tjzxbbs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Tjzxbb tjzxbb : tjzxbbs){
				clearCache(tjzxbb,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Tjzxbb insert(Tjzxbb tjzxbb){
		tjzxbb = TjzxbbDao.insert(tjzxbb);
		if(tjzxbb!=null){
			loadCache(tjzxbb);
		}
    	return tjzxbb;
    }
    
    public static Tjzxbb update(Tjzxbb tjzxbb){
    	tjzxbb = TjzxbbDao.update(tjzxbb);
    	if(tjzxbb!=null){
			loadCache(tjzxbb);
		}
    	return tjzxbb;
    }
    
    public static boolean delete(Tjzxbb tjzxbb){
    	boolean bool = TjzxbbDao.delete(tjzxbb);
    	if(bool){
    		clearCache(tjzxbb);
    	}
    	return bool;
    }
}